/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.core.hibernate;

import com.arg.deploy.bean.User;
import com.arg.deploy.core.common.CoreContent;
import static com.arg.deploy.core.common.CoreContent.VALUE;
import com.arg.deploy.core.common.TransactionalProcessor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author sorasaks
 */
public class UserCore extends CoreContent {

    private static final Logger LOG = Logger.getLogger(UserCore.class);

    public static List<User> getUser() {

        final HashMap param = new HashMap();
        try {
            new TransactionalProcessor() {
                @Override
                public void process(Session session, Transaction tx) {
                    Criteria criteria = session.createCriteria(User.class);
                    criteria.addOrder(Order.asc("username"));
                    param.put(VALUE, criteria.list());
                }
            }.process();
        } catch (HibernateException e) {
            LOG.error(e.getMessage());
            param.put(VALUE, new ArrayList<User>());
        }

        return (List<User>) param.get(VALUE);
    }

    public static User getUserSignin(final String username, final String password) {

        final HashMap param = new HashMap();
        try {
            new TransactionalProcessor() {
                @Override
                public void process(Session session, Transaction tx) {
                    Criteria criteria = session.createCriteria(User.class);
                    criteria.add(Restrictions.eq("username", username));
                    criteria.add(Restrictions.eq("password", password));
                    criteria.setMaxResults(1);

                    User user = (User) criteria.uniqueResult();

                    LOG.info("::::::::::::::: user :::::::::::::::" + user);

                    param.put(VALUE, user);
                }
            }.process();
        } catch (HibernateException e) {
            LOG.error(e.getMessage());
            param.put(VALUE, null);
        }

        return (User) param.get(VALUE);
    }

    public static User getUserByUsername(final String username) {

        final HashMap param = new HashMap();
        try {
            new TransactionalProcessor() {
                @Override
                public void process(Session session, Transaction tx) {
                    Criteria criteria = session.createCriteria(User.class);
                    criteria.add(Restrictions.eq("username", username));
                    criteria.setMaxResults(1);

                    User user = (User) criteria.uniqueResult();

                    LOG.info("::::::::::::::: user :::::::::::::::" + user);

                    param.put(VALUE, user);
                }
            }.process();
        } catch (HibernateException e) {
            LOG.error(e.getMessage());
            param.put(VALUE, null);
        }

        return (User) param.get(VALUE);
    }

    public static Boolean save(final User user) {

        final HashMap param = new HashMap();
        try {
            new TransactionalProcessor() {
                @Override
                public void process(Session session, Transaction tx) {
                    session.save(user);
                    param.put(VALUE, Boolean.TRUE);
                }
            }.process();
        } catch (HibernateException e) {
            LOG.error(e.getMessage());
            param.put(VALUE, Boolean.FALSE);
        }

        return (Boolean) param.get(VALUE);
    }

    public static Boolean update(final User user) {

        final HashMap param = new HashMap();
        try {
            new TransactionalProcessor() {
                @Override
                public void process(Session session, Transaction tx) {
                    session.update(user);
                    param.put(VALUE, Boolean.TRUE);
                }
            }.process();
        } catch (HibernateException e) {
            LOG.error(e.getMessage());
            param.put(VALUE, Boolean.FALSE);
        }

        return (Boolean) param.get(VALUE);
    }

    public static Boolean delete(final User user) {

        final HashMap param = new HashMap();
        try {
            new TransactionalProcessor() {
                @Override
                public void process(Session session, Transaction tx) {
                    session.delete(user);
                    param.put(VALUE, Boolean.TRUE);
                }
            }.process();
        } catch (HibernateException e) {
            LOG.error(e.getMessage());
            param.put(VALUE, Boolean.FALSE);
        }

        return (Boolean) param.get(VALUE);
    }
}
